using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using DbDiff.DbDiff_Common;

namespace DbDiff.DbDiffWinClient.Forms
{
	public class ucDbDiffListView : DbDiff.DbDiffWinClient.Forms.ucDbListBase
	{
		private System.Windows.Forms.ListView lvList;
		private System.ComponentModel.IContainer components = null;

		#region Constructors
		public ucDbDiffListView()
			: base ()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitializeComponent call
		}

		public ucDbDiffListView(ContextMenu contextMenu, int databaseIndex)
			: base(contextMenu, databaseIndex)
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
	
			// TODO: Add any initialization after the InitializeComponent call
		}

		#endregion Constructors

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		
		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lvList = new System.Windows.Forms.ListView();
			this.SuspendLayout();
			// 
			// lvList
			// 
			this.lvList.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lvList.FullRowSelect = true;
			this.lvList.GridLines = true;
			this.lvList.HideSelection = false;
			this.lvList.Location = new System.Drawing.Point(0, 0);
			this.lvList.Name = "lvList";
			this.lvList.Size = new System.Drawing.Size(544, 352);
			this.lvList.TabIndex = 0;
			this.lvList.View = System.Windows.Forms.View.Details;
			this.lvList.Click += new System.EventHandler(this.lvList_Click);
			// 
			// ucDbDiffListView
			// 
			this.Controls.Add(this.lvList);
			this.Name = "ucDbDiffListView";
			this.ResumeLayout(false);

		}
		#endregion

		#region SetData
		public override void SetData(DataTable data, dbObjectsTable itemTypeInfo) 
		{
			_itemTypeInfo = itemTypeInfo;

			lvList.Columns.Clear();
			lvList.Items.Clear();

			if (data != null) 
			{
				ListViewItem listItem;
				string[] items;

				items = new string[data.Columns.Count];
				foreach (DataColumn column in data.Columns) 
				{
					lvList.Columns.Add (column.Caption, 100, HorizontalAlignment.Left);
				}

				foreach (DataRow row in data.Rows) 
				{
					for (int i=0; i<data.Columns.Count; i++) 
					{
						if ((row[i] != null) &&
							(row[i] != DBNull.Value)) 
						{
							items[i] = row[i].ToString();
						}
					}
					listItem = new ListViewItem(items);
					lvList.Items.Add(listItem);
				}
			}
		}
		#endregion SetData

		#region Event handlers
		private void lvList_Click(object sender, System.EventArgs e)
		{
			int rowPosition = -1;

			if (lvList.SelectedIndices.Count > 0) 
			{
				rowPosition = lvList.SelectedIndices[0];
			}

			/*if (rowPosition > -1)
			{
				DataRow dr;
				if (dgDbDiffList.DataSource is DataTable)
				{
					DataTable dt = (DataTable)dgDbDiffList.DataSource;
					dr = dt.DefaultView[rowPosition].Row;
				}
				else if (dgDbDiffList.DataSource is DataView)
				{
					DataView dv = (DataView)dgDbDiffList.DataSource;
					dr = dv[rowPosition].Row;
				}
				else 
				{
					dr = null;
				}
				FireRowSelectEvent(this, new RowClickEventArgs(dr, _itemTypeInfo, _databaseIndex));
			}*/
		}

		/*private void dgDbDiffList_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			int rowPosition = 0;

			CurrencyManager cm = (CurrencyManager)this.BindingContext[dgDbDiffList.DataSource];
			rowPosition = (int)cm.Position;

			if (rowPosition > -1)
			{
				DataRow dr;
				if (dgDbDiffList.DataSource is DataTable)
				{
					DataTable dt = (DataTable)dgDbDiffList.DataSource;
					dr = dt.DefaultView[rowPosition].Row;
				}
				else if (dgDbDiffList.DataSource is DataView)
				{
					DataView dv = (DataView)dgDbDiffList.DataSource;
					dr = dv[rowPosition].Row;
				}
				else 
				{
					dr = null;
				}
				FireRowSelectEvent(this, new RowClickEventArgs(dr, _itemTypeInfo, _databaseIndex));
			}
		}

		private void dgDbDiffList_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
		{
		}
		private void dgDbDiffList_DoubleClick(object sender, System.EventArgs e)
		{
			int rowPosition = 0;

			CurrencyManager cm = (CurrencyManager)this.BindingContext[dgDbDiffList.DataSource];
			rowPosition = (int)cm.Position;

			if (rowPosition > -1)
			{
				DataRow dr;
				if (dgDbDiffList.DataSource is DataTable)
				{
					DataTable dt = (DataTable)dgDbDiffList.DataSource;
					dr = dt.DefaultView[rowPosition].Row;
				}
				else if (dgDbDiffList.DataSource is DataView)
				{
					DataView dv = (DataView)dgDbDiffList.DataSource;
					dr = dv[rowPosition].Row;
				}
				else 
				{
					dr = null;
				}
				FireRowDoubleClickEvent(this, new RowClickEventArgs(dr, _itemTypeInfo, _databaseIndex));
			}
		}*/
		#endregion Event handlers
	}
}

